﻿using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FCT551.Models;
using OfficeOpenXml.FormulaParsing.Exceptions;

namespace FCT551.Helper
{
    public static class ExcelHelper
    {
        public static Dictionary<string, List<object[]>> _sheetdata;

        /// <summary>
        /// 读取excel里每一个sheet
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static void ReadAllSheets(string filePath)
        {
            _sheetdata = new Dictionary<string, List<object[]>>();

            try
            {
                FileInfo fileInfo = new FileInfo(filePath);
                using (var package = new ExcelPackage(fileInfo))
                {
                    foreach (var worksheet in package.Workbook.Worksheets)
                    {
                        var sheetName = worksheet.Name;
                        var data = new List<object[]>();

                        int rowCount = worksheet.Dimension.Rows;
                        int colCount = worksheet.Dimension.Columns;

                        for (int row = 1; row <= rowCount; row++)
                        {
                            var rowData = new object[colCount];
                            for (int col = 1; col <= colCount; col++)
                            {
                                rowData[col - 1] = worksheet.Cells[row, col].Value;
                            }

                            data.Add(rowData);
                        }

                        _sheetdata.Add(sheetName, data);
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 读取IO定义sheet
        /// </summary>
        public static void ReadIOSheet()
        {
            try
            {
                Global._IOconfigs = new();
                var config = _sheetdata[Global._IOconfigName];
                config.RemoveAt(0);
                foreach (var item in config)
                {
                    var key = item[0].ToString();
                    var index = item[1].ToString();

                    Global._IOconfigs.Add(key, index);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 读取测试流程sheet
        /// </summary>
        public static void ReadTestProcedureSheet()
        {
            try
            {
                Global._TestProcedureConfigs = new();

                var config = _sheetdata[Global._TestProcedureconfigName];
                config.RemoveAt(0);
                foreach (var item in config)
                {
                    TestMethodModel Config = new TestMethodModel
                    {
                        Group = Convert.ToInt32(item[0]?.ToString()),
                        ProcessName = item[1]?.ToString(),
                        type = item[2]?.ToString() == "设置方法" ? ProcessType.设置方法 : ProcessType.读取方法,
                        ExecutionSequence = Convert.ToInt32(item[3]?.ToString()),
                        IfFalseContinue = item[4]?.ToString(),
                        IsRun = item[5]?.ToString(),
                        TestCondition = item[6]?.ToString(),
                        TestConfig = item[7]?.ToString(),
                        TargetProcess = item[8]?.ToString(),
                        ReturnSign = item[9]?.ToString(),
                        ComparisonSign = item[10]?.ToString(),
                        item1 = item[11]?.ToString(),
                        item2 = item[12]?.ToString(),
                        item3 = item[13]?.ToString(),
                        item4 = item[14]?.ToString(),
                        item5 = item[15]?.ToString(),
                        item6 = item[16]?.ToString(),
                        item7 = item[17]?.ToString(),
                        item8 = item[18]?.ToString(),
                        item9 = item[19]?.ToString(),
                        item10 = item[20]?.ToString(),
                        item11 = item[21]?.ToString(),
                        item12 = item[22]?.ToString(),
                        item13 = item[23]?.ToString(),
                        item14 = item[24]?.ToString(),
                        item15 = item[25]?.ToString(),
                        item16 = item[26]?.ToString(),
                        item17 = item[27]?.ToString(),
                        item18 = item[28]?.ToString(),
                        item19 = item[29]?.ToString(),
                        item20 = item[30]?.ToString(),
                        item21 = item[31]?.ToString(),
                        item22 = item[32]?.ToString(),
                        item23 = item[33]?.ToString(),
                        item24 = item[34]?.ToString(),
                        item25 = item[35]?.ToString(),
                        item26 = item[36]?.ToString(),
                        item27 = item[37]?.ToString(),
                        item28 = item[38]?.ToString(),
                        item29 = item[39]?.ToString(),
                        item30 = item[40]?.ToString(),
                        item31 = item[41]?.ToString(),
                        item32 = item[42]?.ToString(),
                        item33 = item[43]?.ToString(),
                        item34 = item[44]?.ToString(),
                        item35 = item[45]?.ToString(),
                        item36 = item[46]?.ToString(),
                        item37 = item[47]?.ToString(),
                        item38 = item[48]?.ToString(),
                        item39 = item[49]?.ToString(),
                        item40 = item[50]?.ToString()
                    };

                    Global._TestProcedureConfigs.Add(Config);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 读取测试方法sheet,仅说明，不启用
        /// </summary>
        public static void ReadTestMethodsSheet()
        {
        }

        /// <summary>
        /// 读取测试配置sheet,转化成字典,加载补偿
        /// </summary>
        public static void ReadTestConfigSheet()
        {
            try
            {
                Global._TestConfigs = new();
                var config = _sheetdata[Global._TestconfigName];
                config.RemoveAt(0);
                foreach (var item in config)
                {
                    var key = item[0].ToString();
                    var value = Convert.ToInt32(item[1].ToString());

                    Global._TestConfigs.Add(key, value);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 加载产品配置
        /// </summary>
        public static void ReadProduceConfig()
        {
            try
            {
                Global._ProduceConfigs = new();
                var config = _sheetdata[Global._ProduceConfigName];
                config.RemoveAt(0);
                foreach (var item in config)
                {
                    ProduceConfigModel model = new ProduceConfigModel();
                    model.Id = Convert.ToInt32(item[0]);
                    model.IsTest = item[1].ToString() == "1" ? true : false;
                    model.TimeOut = Convert.ToInt32(item[2]);
                    model.RotateSpeedDown = Convert.ToDouble(item[3]);
                    model.RotateSpeedUp = Convert.ToDouble(item[4]);
                    model.VoltageDown = Convert.ToDouble(item[5]);
                    model.VoltageUp = Convert.ToDouble(item[6]);
                    model.RotateSpeedOffset = Convert.ToInt32(item[7]);
                    model.VoltageOffset = Convert.ToDouble(item[8]);
                    model.PWMValue = Convert.ToInt32(item[9]);
                    model.PWMValueNums = Convert.ToInt32(item[10]);
                    model.PWMCloseValue = Convert.ToInt32(item[11]);
                    Global._ProduceConfigs.Add(model);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}